package com.ease.common.reflect;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

public class TypeReflectSample2<E> {

	public static <T> Class<?> fun(List<T> list) {
		Type genericSuperclass = list.getClass().getGenericSuperclass();
		if (genericSuperclass instanceof ParameterizedType) {
			ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
			Type actualTypeArgument = (Type) parameterizedType.getActualTypeArguments()[0];
			if (actualTypeArgument instanceof Class) {
				return (Class<?>) actualTypeArgument;
			}
		}

		return Object.class;
	}

	public static <T> Class<?> fun2(List<T> list) {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(list.getClass());
		enhancer.setCallbacks(new Callback[] { NoOp.INSTANCE });
		// enhancer.setCallbackFilter(new AuthProxyFilter());

		return fun((List<T>) enhancer.create());
	}

	public static void main(String[] args) {
		System.out.println(Long.class.equals(fun(new ArrayList<Long>() {
		})));
		System.out.println(Long.class.equals(fun2(new ArrayList<Long>())));

		// p("---------------------->");
		// p(new TypeReflectSample<Object>() { }.getClass().getGenericSuperclass() instanceof ParameterizedType);
		// p(new TypeReflectSample<Object>().getClass().getGenericSuperclass() instanceof ParameterizedType);
		//
		// Class<?> typeReflectSampleObjectClass= new TypeReflectSample<Object>().getClass();
		// TypeVariable<?> typeVariableClass= typeReflectSampleObjectClass.getTypeParameters()[0];
		// p("Class: " + typeVariableClass.getClass() + " Type: " + typeVariableClass);
		// p("Class: " + typeVariableClass.getGenericDeclaration().getClass() + " Type: " +
		// typeVariableClass.getGenericDeclaration());
		// p("---------------------->");
		// p(new ParameterizedBeanPropertyRowMapper<Endpoint>().getClass().getGenericSuperclass() instanceof
		// ParameterizedType);
		// p(new ParameterizedBeanPropertyRowMapper<Endpoint>().getClass().getSuperclass().getGenericSuperclass()
		// instanceof ParameterizedType);
		//
		// p(ReflectionUtil.getGenericType(new ParameterizedBeanPropertyRowMapper<Endpoint>().getClass()));
		//
	}
}
